package com.allwees.bs.c.module.app.controller;

import com.allwees.bs.c.module.app.config.SiteInfoProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Daniel
 * @version 1.0.0
 * @since 2020/12/6 13:14
 */
@Component
@Slf4j
public class SiteInfoHelper {

    private static volatile DbSearcher dbSearcher;

    private static String dbPath;
    static {
        dbPath = "/ip2region.db";
        File file = new File(dbPath);
        if (!file.exists()) {
            try {
                FileUtils.copyInputStreamToFile(SiteInfoHelper.class.getResourceAsStream("/ip2region.db"), file);
            } catch (IOException e) {
                log.error("load ip2region.db file error~");
            }
        }
    }

    @Value("${spring.profiles.active}")
    private String profile;

    @Autowired
    private SiteInfoProperties properties;

    public Map<String, Object> getInfo(String ip) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> country = new HashMap<>();
        Map<String, Object> currency = new HashMap<>();
        map.put("country", country);
        map.put("currency", currency);
        try {
            String region = getDbSearcher().memorySearch(ip).getRegion();
            log.info("===>site info,region:{}",region);
            if (region.contains("沙特阿拉伯")) {
                setSa(map, country, currency);
                return map;
            }

            if (region.contains("阿联酋")) {
                setAe(map, country, currency);
                return map;
            }

            if (region.contains("美国")) {
                 setUs(map, country, currency);
                return map;
            }
            //采用香港ip测试AE站
            if (region.contains("香港")) {
                setAe(map, country, currency);
                return map;
            }
        } catch (Exception e) {
            log.error("===>get county error:{}",e.getMessage(),e);
        }

        //欧美测试环境默认英语方便调试
        if ("test".equals(profile)) {
            setUs(map, country, currency);
            return map;
        }
        //其他国家默认阿拉伯语
        setSa(map, country, currency);
        return map;
    }

    private void setUs(Map<String, Object> map,Map<String, Object> country,Map<String, Object> currency){
        country.put("nameEn", "United States");
        country.put("region", "53950000");
        country.put("language", "en");
        map.put("host",properties.getUs().getHost());
        map.put("cdn",properties.getUs().getCdn());
        currency.put("abbr",properties.getUs().getAbbr());
        currency.put("symbol",properties.getUs().getSymbol());
    }

    private void setSa(Map<String, Object> map,Map<String, Object> country,Map<String, Object> currency){
        country.put("nameEn", "المملكة العربية السعودية");
        country.put("region", "48650000");
        country.put("language", "ar");
        map.put("host",properties.getSa().getHost());
        map.put("cdn",properties.getSa().getCdn());
        currency.put("abbr",properties.getSa().getAbbr());
        currency.put("symbol",properties.getSa().getSymbol());
    }


    private void setAe(Map<String, Object> map,Map<String, Object> country,Map<String, Object> currency){
        country.put("nameEn", "الإمارات العربية المتحدة");
        country.put("region", "2610000");
        country.put("language", "ar");
        map.put("host",properties.getAe().getHost());
        map.put("cdn",properties.getAe().getCdn());
        currency.put("abbr",properties.getAe().getAbbr());
        currency.put("symbol",properties.getAe().getSymbol());
    }

    private static DbSearcher getDbSearcher() throws Exception {
        if (dbSearcher == null) {
            synchronized (SiteInfoHelper.class) {
                if (dbSearcher == null) {
                    DbConfig config = new DbConfig();
                    dbSearcher = new DbSearcher(config, dbPath);
                }
            }
        }
        return dbSearcher;
    }
}
